package com.ys.service.finance.impl;

import com.ys.entity.business.TbBaseInfo;
import com.ys.entity.finance.TbCompanyBankInfoAccount;
import com.ys.entity.finance.TcwFinancialTransactionFlow;
import com.ys.entity.finance.TcwFinancialTransactionTypeItem;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.mapper.finance.TbCompanyBankInfoAccountMapper;
import com.ys.mapper.finance.TcwFinancialTransactionFlowMapper;
import com.ys.service.finance.TcwFinancialTransactionFlowService;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 财务交易流水
 */
@Service("tcwFinancialTransactionFlowService")
public class TcwFinancialTransactionFlowServiceImpl  implements TcwFinancialTransactionFlowService{
	private static final Logger logger = LoggerFactory.getLogger(TcwFinancialTransactionFlowServiceImpl.class);

	@Autowired
	private TcwFinancialTransactionFlowMapper tcwFinancialTransactionFlowMapper;

	@Autowired
	private TbCompanyBankInfoAccountMapper tbCompanyBankInfoAccountMapper;

	/**
	 * 判断金额是否充足；新增流水、并修改对应公司银行账户实时资金管理
	 * @param isCheckCapital 是否判断资金是否充足，是true、否false（若为否，并扣除资金可能为负）
	 * @param companyBankInfoId 公司银行账户管理表id（TB_CompanyBankInfo）
	 * @param transactionTypeBid 业务交易类型bid（TB_BaseInfo）
	 * @param vyhzhmc 对方账户名称
	 * @param vkhh 对方账户开户行
	 * @param vyhzh 对方银行账户编码
	 * @param originalTransactionAmount 交易金额
	 * @param occurTime 交易发生时间
	 * @param auditingTime 交易审核时间
	 * @param writeTime 记账时间
	 * @param remark 备注
	 * @param fromType 来源表单类型(见枚举类：TcwFinancialTransactionFlowEnum)
	 * @param fromId 来源表单id
	 * @return R SUCCESS("成功", 0), FAILED("失败", 1); 失败原因：R.getMsg()
	 * @throws Exception
	 */
	@Override
	//@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R add(boolean isCheckCapital, Long companyBankInfoId, String transactionTypeBid,
				 String vyhzhmc, String vkhh, String vyhzh, BigDecimal originalTransactionAmount,
				 Date occurTime, Date auditingTime, Date writeTime, String remark,
				 int fromType, Long fromId) throws Exception {

		TbCompanyBankInfo tbCompanyBankInfo = new TbCompanyBankInfo().setId(companyBankInfoId).queryFirst();
		if(tbCompanyBankInfo!=null){
			TbCompanyBankInfoAccount tbCompanyBankInfoAccount = new TbCompanyBankInfoAccount().setCompanyBankInfoVid(tbCompanyBankInfo.getVid()).queryFirst();
			if(tbCompanyBankInfoAccount!=null){
				//判断资金是否充足
				BigDecimal fjeLeft = tbCompanyBankInfoAccount.getAccount().add(originalTransactionAmount);//余额
				if(isCheckCapital && fjeLeft.compareTo(BigDecimal.ZERO)<0 && originalTransactionAmount.compareTo(BigDecimal.ZERO)<0){
					return R.error("该公司"+tbCompanyBankInfo.getCompanyInfoVid()+"的【账户编码："+tbCompanyBankInfo.getVid()+"；开户行："+tbCompanyBankInfo.getVkhh()+"】的银行账户资金不足");
				}

				//新增流水
				TcwFinancialTransactionFlow entity = new TcwFinancialTransactionFlow();
				entity.setCompanyBankInfoId(companyBankInfoId);
				entity.setTransactionTypeBid(transactionTypeBid);
				entity.setVyhzhmc(vyhzhmc);
				entity.setVkhh(vkhh);
				entity.setVyhzh(vyhzh);
				entity.setOriginalTransactionAmount(originalTransactionAmount);
				entity.setOccurTime(occurTime);
				entity.setAuditingTime(auditingTime);
				entity.setWriteTime(writeTime);
				entity.setRemark(remark);
				entity.setFjeLeft(fjeLeft);
				entity.setFromType(fromType);
				entity.setFromId(fromId);
				entity.setStatus(1);
				tcwFinancialTransactionFlowMapper.insert(entity);

				//修改对应万里汇资金信息
				tbCompanyBankInfoAccount.setAccount(fjeLeft);
				tbCompanyBankInfoAccountMapper.update(tbCompanyBankInfoAccount);

			}else{
				return R.error("未查询到对应的公司银行账户实时资金管理信息");
			}
		}else{
			return R.error("未查询到对应的公司银行账户管理信息");
		}

		return R.ok();
	}


	@Override
	public TbBaseInfo getTransactionType(String transactionTypeStr) throws Exception{

		TbBaseInfo tbBaseInfo = new TbBaseInfo().setVReMark(transactionTypeStr).queryFirst();
		return tbBaseInfo;

		/*
		if(transactionTypeItem!=null && transactionTypeItem.getId()!=null){

		}

		 */

	}




	/**
	 * 判断资金是否充足
	 * @param companyBankInfoId 公司银行账户管理表id（TB_CompanyBankInfo）
	 * @param originalTransactionAmount 交易金额（一般为负）
	 * @return 是：true ； 否false
	 */
	@Override
	public boolean isCapital(Long companyBankInfoId,BigDecimal originalTransactionAmount) throws Exception{
		boolean isCapital = false;
		TbCompanyBankInfo tbCompanyBankInfo = new TbCompanyBankInfo().setId(companyBankInfoId).queryFirst();
		if(tbCompanyBankInfo!=null){
			TbCompanyBankInfoAccount tbCompanyBankInfoAccount = new TbCompanyBankInfoAccount().setCompanyBankInfoVid(tbCompanyBankInfo.getVid()).queryFirst();
			if(tbCompanyBankInfoAccount!=null) {
				//判断资金是否充足
				BigDecimal fjeLeft = tbCompanyBankInfoAccount.getAccount().add(originalTransactionAmount);//余额
				if (fjeLeft.compareTo(BigDecimal.ZERO) < 0 && originalTransactionAmount.compareTo(BigDecimal.ZERO) < 0) {
					isCapital = false;
				}else {
					isCapital = true;
				}
			}
		}

		return isCapital;



	}


}
